using System;
using System.Collections.Generic;
using System.Linq;

namespace ReactiveListUnitTest.Entities
{
    public class Company : Entity
    {
        public Name Name
        {
            get => DataContext.Names.FirstOrDefault(Name => Name.Id == __Name);

            set
            {
                __Name = value.Id;
                if (!DataContext.Names.Any(item => item.Id == __Name)) DataContext.Names.Add(value);
            }
        }

        protected String __Name { get; set; }

        public Contacts Contacts
        {
            get => DataContext.Contactses.FirstOrDefault(Contacts => Contacts.Id == __Contacts);
            set
            {
                __Contacts = value.Id;
                if (!DataContext.Contactses.Any(item => item.Id == __Contacts)) DataContext.Contactses.Add(value);
            }
        }

        protected String __Contacts { get; set; }
        public String Address { get; set; }

        public Person[] Persons
        {
            get => __Persons.Select(item1 => DataContext.Persons.Single(item2 => item1 == item2.Id)).ToArray();
            set => __Persons = value.Select(item => item.Id).ToArray();
        }

        protected String[] __Persons { get; set; }
        public override string Id { get; set; } = Guid.NewGuid().ToString();
    }
}